package com.bw.coupon.timing;

import com.bw.common.core.utils.StringUtils;
import com.bw.common.redis.service.RedisService;
import com.bw.coupon.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Component
public class Synchronous {
    @Autowired
    private RedisService redisService;
    @Autowired
    private OrderService orderService;

    private static final String CART_ITEM = "inventory:num:";
    // 定时同步库存数量
    @Scheduled(cron = "0 0/1 * * * ?")//每分钟执行一次
    public void synchronousInventory() {

        Collection<String> keys = redisService.keys("inventory:num:*");

        for (String key : keys) {
            String[] split = key.split(":");

            Integer skuId = Integer.valueOf(split[split.length - 1]);

            Integer inventory = redisService.getCacheObject(CART_ITEM + skuId);

            orderService.updateInventory(skuId,inventory);

        }



    }
}
